package com.kaishengit.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.kaishengit.pojo.Goal;
import com.kaishengit.pojo.Project;
import com.kaishengit.pojo.Task;
import com.kaishengit.pojo.User;
import com.kaishengit.service.GoalService;
import com.kaishengit.service.TaskService;

@Controller
@Results({
		@Result(name = "addSuccess", location = "goal.action", type = "redirectAction"),
		@Result(name = "editGoalSuccess", location = "goal.action", type = "redirectAction") })
public class GoalAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private GoalService goalService;
	private TaskService taskService;

	private List<Goal> goalList;
	private List<Task> taskList;
	private Goal goal;
	private User user;
	private Project project;

	private String id;
	private int size;
	private int rates;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	@Action("goal")
	public String goalMain() {
		project=(Project) getFromSession("project");
		goalList = goalService.finAll(project);
		for (Goal g : goalList) {
			rates = 0;
			taskList = taskService.finAllTask(g);
			if (taskList.size() != 0) {
				for (Task t : taskList) {
					rates += t.getRate();
					
						size = taskList.size();
				}
				rates = rates / size;
				g.setGoalrates(rates);
			}else{
				rates=0;
			}
		}
		System.out.println(rates);
		return SUCCESS;
	}

	@Action("toaddgoal")
	public String toAddGoal() {

		return SUCCESS;
	}

	@Action("savegoal")
	public String save() {
		user = (User) getFromSession("user");
		project = (Project) getFromSession("project");
		goalService.save(goal, user, project);
		return "addSuccess";
	}

	@Action("toeditgoal")
	public String edit() {
		goal = goalService.findById(id);
		return SUCCESS;
	}

	@Action("editgoal")
	public String editGoal() {
		goalService.update(goal, id);
		return "editGoalSuccess";
	}

	@Autowired
	public void setGoalService(GoalService goalService) {
		this.goalService = goalService;
	}

	@Autowired
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public List<Goal> getGoalList() {
		return goalList;
	}

	public void setGoalList(List<Goal> goalList) {
		this.goalList = goalList;
	}

	public Goal getGoal() {
		return goal;
	}

	public void setGoal(Goal goal) {
		this.goal = goal;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getRates() {
		return rates;
	}

	public void setRates(int rates) {
		this.rates = rates;
	}

	public List<Task> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

}
